package com.aspose.words.maven;

import java.net.URI;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import com.aspose.words.Activator;
import com.aspose.words.maven.utils.AsposeJavaAPI;
import com.aspose.words.maven.utils.AsposeMavenProjectManager;
import com.aspose.words.maven.utils.AsposeWordsJavaAPI;
import java.io.File;
import java.io.IOException;

public class AsposeMavenProjectSupport {

	public static IProject createProject(String projectName, URI location, String packageName,
			boolean downloadExamples) {
		IProject project = createBaseProject(projectName, location);
		try {

			if (downloadExamples) {
				AsposeMavenProjectManager.initialize(new File(location));
				AsposeJavaAPI component = AsposeWordsJavaAPI.initialize(AsposeMavenProjectManager.getInstance());
				component.checkAndUpdateRepo();
			}

			String[] paths = { "src/main/java/" + packageName.replace(".", "/"), "src/test/java" };
			addToProjectStructure(project, paths);

			Files.copy(new File(Activator.getResourceFilePath("pom-xml-template.txt")).toPath(),
					new File(location.getPath() + "/pom.xml").toPath());
			Files.copy(new File(Activator.getResourceFilePath("classpath-template.txt")).toPath(),
					new File(location.getPath() + "/.classpath").toPath());
			Files.copy(new File(Activator.getResourceFilePath("project-template.txt")).toPath(),
					new File(location.getPath() + "/.project").toPath(), StandardCopyOption.REPLACE_EXISTING);

			project.refreshLocal(IResource.DEPTH_INFINITE, null);

		} catch (CoreException e) {
			e.printStackTrace();
			project = null;
		} catch (IOException e) {
			e.printStackTrace();
		}

		return project;
	}

	private static IProject createBaseProject(String projectName, URI location) {
		IProject newProject = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);

		if (!newProject.exists()) {
			URI projectLocation = location;
			IProjectDescription desc = newProject.getWorkspace().newProjectDescription(newProject.getName());
			if (location != null && ResourcesPlugin.getWorkspace().getRoot().getLocationURI().equals(location)) {
				projectLocation = null;
			}

			desc.setLocationURI(projectLocation);
			try {
				newProject.create(desc, null);
				if (!newProject.isOpen()) {
					newProject.open(null);
				}
			} catch (CoreException e) {
				e.printStackTrace();
			}
		}

		return newProject;
	}

	private static void createFolder(IFolder folder) throws CoreException {
		IContainer parent = folder.getParent();
		if (parent instanceof IFolder) {
			createFolder((IFolder) parent);
		}
		if (!folder.exists()) {
			folder.create(false, true, null);
		}
	}

	/**
	 * Create a folder structure with a parent root, overlay, and a few child
	 * folders.
	 *
	 * @param newProject
	 * @param paths
	 * @throws CoreException
	 */
	private static void addToProjectStructure(IProject newProject, String[] paths) throws CoreException {
		for (String path : paths) {
			IFolder etcFolders = newProject.getFolder(path);
			createFolder(etcFolders);
		}
	}

}